package com.urick.statemachine;

import java.util.HashMap;
import java.util.Map;

/**
 * Design Theories: 状态机流程流转关系映射
 * <a href=""></a>
 *
 * @author Rick 2023/4/4 16:24
 * Email: rickwork@163.com
 * Wechat: yy3076815642
 * QQ: 3076815642
 * @since 1.0
 */
public class StateMap {
    private final Map<Object, IStateProcess> event;
    private final Object currentState;
    private final Object nextState;

    public StateMap(Object currentState, Object nextState, Map<Object, IStateProcess> event) {
        this.currentState = currentState;
        this.nextState = nextState;
        this.event = event;
    }

    public Map<Object, IStateProcess> getEvent() {
        return event;
    }

    public boolean contains(Object currentEvent) {
        return getEvent().containsKey(currentEvent);
    }

    public Object getCurrentState() {
        return currentState;
    }

    public Object getNextState() {
        return nextState;
    }

    public IStateProcess getProcess(Object currentEvent) {
        return event.get(currentEvent);
    }


    public static StateMapBuailder builder() {
        return new StateMapBuailder();
    }

    public static class StateMapBuailder {
        /**
         * 源状态
         */
        private Object fromState;
        private final Map<Object, IStateProcess> eventMap = new HashMap<>();
        /**
         * 源状态，事件处理完成流转的下一个状态
         */
        private Object toState;


        public StateMapBuailder from(Object fromState) {
            this.fromState = fromState;
            return this;
        }

        /**
         * 事件状态流程，流转映射
         *
         * @param triggerEvent event
         * @param stateProcess process
         */
        public StateMapBuailder eventMap(Object triggerEvent, IStateProcess stateProcess) {
            eventMap.put(triggerEvent, stateProcess);
            return this;
        }

        public StateMapBuailder to(Object toState) {
            this.toState = toState;
            return this;
        }

        public StateMap build() {
            return new StateMap(fromState, toState, eventMap);
        }

    }

}
